﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using System.Xml.Linq;
using MyOA.BLL;
using MyOA.Model;


namespace MyOA.UI.ManualSign
{
    public partial class ManualSignSearch : System.Web.UI.Page
    {

        ManualSignManager manualSignManager = new ManualSignManager();
        ManualSignSearchManager manualSignSearchManager = new ManualSignSearchManager();
        BranchInfoManager branchInfoManager = new BranchInfoManager();
        private int Pager
        {
            set { ViewState["page"] = value; }
            get { return (int)ViewState["page"]; }
        }
        private IList<MyOA.Model.ManualSign> ManualSign
        {
            set { ViewState["manualSign"] = value; }
            get { return (IList<MyOA.Model.ManualSign>)ViewState["manualSign"]; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["loginId"] == null)
            {
                Response.Redirect("~/Login.aspx");
                return;
            }
            if (!IsPostBack)
            {
                Pager = 1;
                IList<BranchInfo> branchs = branchInfoManager.GetBranchName();
                BranchInfo bi = new BranchInfo();
                bi.BranchName = "==请选择==";
                bi.BranchId = -1;
                branchs.Add(bi);
                // branchs.Reverse();  
                ddlBranchs.DataTextField = "BranchName";
                ddlBranchs.DataValueField = "BranchId";
                ddlBranchs.DataSource = branchs;
              
                //选中项的索引
                ddlBranchs.SelectedIndex = branchs.Count - 1;
                ddlBranchs.DataBind();


                ddlBranchs.Enabled = false;
                ddlDeparts.Enabled = false;
                ddlDeparts.Enabled = false;
                txtLoginId.Enabled = false;
            }

        }


        //根据不同的机构,列出不同的部门
        protected void chklstSelect_SelectedIndexChanged(object sender, EventArgs e)
        {


            if (chklstSelect.Items[0].Selected == true)
            {
                ddlBranchs.Enabled = true;
                ddlBranchs.BackColor = Color.Azure;
            }


            if (chklstSelect.Items[1].Selected == true)
            {
                chklstSelect.Items[0].Selected = true;
                ddlBranchs.Enabled = true;
                ddlDeparts.Enabled = true;
                ddlDeparts.BackColor = Color.Azure;

                int branchId = Convert.ToInt32(ddlBranchs.SelectedValue);
                ddlDeparts.DataTextField = "DepartName";
                ddlDeparts.DataSource = dataBindDepart(branchId);
                ddlDeparts.DataBind();
            }
            else
            {
                ddlDeparts.Enabled = false;
                //ddlDeparts.SelectedIndex = -1;
            }

            if (chklstSelect.Items[2].Selected == true)
            {
                txtLoginId.Enabled = true;
                txtLoginId.Text = "";
                txtLoginId.BackColor = Color.Bisque;
            }
            else
            {
                txtLoginId.Enabled = false;
                txtLoginId.Text = "请输入员工号......";
            }

            if (chklstSelect.Items[3].Selected == true)
            {
                txtUserName.Enabled = true;
                txtUserName.Text = "";
                txtUserName.BackColor = Color.Bisque;
            }
            else
            {
                txtUserName.Enabled = false;
                //txtUserName.Text = "请输入姓名......";
            }
        }

        private IList<DepartInfo> dataBindDepart(int branchId)
        {
            DepartInfoManager dim = new DepartInfoManager();
            return dim.GetDepartName(branchId);
        }
        //根据不同条件搜索
        protected void imgbtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            gvUserSignInfo.DataSource = null;
            DisplayUserSignInfo();
        }

        private void DisplayUserSignInfo()
        {
            DateTime startTime = new DateTime(1,1,1);
            DateTime endTime = new DateTime(1, 1, 1);
            if (txtBeginTime.Text != string.Empty && txtEndTime.Text != string.Empty)
            {
                 startTime = Convert.ToDateTime(this.txtBeginTime.Text + " 00:00:00");
                 endTime = Convert.ToDateTime(this.txtEndTime.Text + " 23:59:59");
            }
            string loginId = this.txtLoginId.Text;//得到输入的员工号
            string userName = this.txtUserName.Text;//得到输入的用户名

            string departName = this.ddlDeparts.SelectedValue;//得到部门
            string branchName = this.ddlBranchs.SelectedItem.Text;//得到机构

             IList<MyOA.Model.ManualSign> manualSign = manualSignSearchManager.GetManualSignsBySelectedConditions(startTime, endTime, loginId, userName, departName, branchName);
        //  IList<MyOA.Model.ManualSign> manualSign = manualSignSearchManager.GetManualSignsBySelectedConditions(loginId);
             this.ManualSign = manualSign;

            //实例化数据化对象
            PagedDataSource pgDataSource = new PagedDataSource();
            pgDataSource.DataSource = manualSign;
            anpDepartInfo.RecordCount = manualSign.Count();
            pgDataSource.AllowPaging = true;    //启用分页
            pgDataSource.PageSize = 5;  //单页显示的项数
            pgDataSource.CurrentPageIndex = Pager - 1;  //获取当前的索引
            this.gvUserSignInfo.DataSourceID = null;
            this.gvUserSignInfo.DataSource = pgDataSource;
            this.gvUserSignInfo.DataBind();

            //Unite(gvUserSignInfo);
        }

        protected void anpDepartInfo_PageChanged(object src, EventArgs e)
        {
            Pager = anpDepartInfo.CurrentPageIndex;
            dataBind();
        }
        //数据绑定方法
        private void dataBind()
        {
            //实例化数据化对象
            PagedDataSource pgDataSource = new PagedDataSource();
            pgDataSource.DataSource = this.ManualSign;
            anpDepartInfo.RecordCount = this.ManualSign.Count();
            pgDataSource.AllowPaging = true;    //启用分页
            pgDataSource.PageSize = 5;  //单页显示的项数
            pgDataSource.CurrentPageIndex = Pager - 1;  //获取当前的索引
            this.gvUserSignInfo.DataSourceID = null;
            this.gvUserSignInfo.DataSource = pgDataSource;
            this.gvUserSignInfo.DataBind();
           // Unite(gvUserSignInfo);
        }



        #region 合并单元格
        protected void Unite(GridView gv)
        {
            int i;
            string LastType1;
            int LastCell;
            if (gv.Rows.Count > 0)
            {
                for (int j = 0; j < 6; j++)
                {
                    if (j != 1 && j != 2 && j != 3)
                    {
                        LastType1 = gv.Rows[0].Cells[j].Text;
                        gv.Rows[0].Cells[j].RowSpan = 1;
                        LastCell = 0;

                        for (i = 1; i < gv.Rows.Count; i++)
                        {
                            if (gv.Rows[i].Cells[j].Text == LastType1)
                            {
                                gv.Rows[i].Cells[j].Visible = false;
                                gv.Rows[LastCell].Cells[j].RowSpan++;
                            }
                            else
                            {
                                LastType1 = gv.Rows[i].Cells[j].Text;
                                LastCell = i;
                                gv.Rows[i].Cells[j].RowSpan = 1;
                            }
                        }
                    }
                }
            }
        }
        #endregion
    }
}

